<?php
class newproject extends controller{
	public $customers;

	public function __construct($url){
		parent::__construct($url, true);
		$this->customers = null;
	}

	public function check(){
		if($_SESSION["user"][5] < 3) $this->redirect("projects", "Nincs engedélye ehhez a funkcióhoz!");
	}

	public function run(){
		$this->getCustomers();
		$this->newproject();
	}

	private function getCustomers(){
		$sql = "SELECT id, name FROM user WHERE status <> 2 AND status > 0";
		$this->customers = $this->database->fetchRow($sql);
	}

	private function newproject(){
		if(isset($_POST["newproject"])){
			if(!empty($_POST["name"]) && !empty($_POST["descr"]) && !empty($_POST["link"]) && !empty($_POST["customer"])){
				$sql = "INSERT INTO project(name, descr, link, customer, status)
						VALUES('".$_POST["name"]."', '".$_POST["descr"]."', '".$_POST["link"]."', ".$_POST["customer"].", 1)";
				if($this->database->query($sql)){
					mkdir("../projects/".$_POST["link"]);
					$this->redirect("start", "A projekt létrehozása sikeres volt.");
				}
				else{
					$this->message = "Adatbázis hiba történt a projekt létrehozása közben!";
				}
			}
			else{
				$this->message = "Üresen hagyott mező!";
			}
		}
	}
}
?>